﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;

/// <summary>
///WebUtility 的摘要说明
/// </summary>
public static class WebUtility
{
    public static void MappingConsumerView(Control control, DAL.BI_Consumer Consumer)
    {
        if (Consumer != null)
        {
            SetValue(control, "Address", Consumer.Address);
            SetValue(control, "Car", Consumer.Car);
            SetValue(control, "CName", Consumer.CName);
            SetValue(control, "Company", Consumer.Company);
            SetValue(control, "Department", Consumer.Department);
            SetValue(control, "Favor", Consumer.Favor);
            SetValue(control, "IsVip", (Consumer.IsVip.Value == 1 ? "是" : "否"));
            SetValue(control, "PhoneNum", Consumer.PhoneNum);
            SetValue(control, "Position", Consumer.Position);
            SetValue(control, "Remark", Consumer.Remark);
            SetValue(control, "Sex", (Consumer.Sex.Value == 1 ? "男" : "女"));
        }
        else
        {
            SetValue(control, "Address", string.Empty);
            SetValue(control, "Car", string.Empty);
            SetValue(control, "CName", string.Empty);
            SetValue(control, "Company", string.Empty);
            SetValue(control, "Department", string.Empty);
            SetValue(control, "Favor", string.Empty);
            SetValue(control, "IsVip", string.Empty);
            SetValue(control, "PhoneNum", string.Empty);
            SetValue(control, "Position", string.Empty);
            SetValue(control, "Remark", string.Empty);
            SetValue(control, "Sex", string.Empty);
        }
    }

    static void SetValue(Control control, string ControlEndName, string value)
    {
        DevExpress.Web.ASPxEditors.ASPxLabel lbel =
            control.FindControl(string.Format("View_{0}", ControlEndName)) as DevExpress.Web.ASPxEditors.ASPxLabel;
        if (lbel != null)
        {
            lbel.Font.Size = System.Web.UI.WebControls.FontUnit.Parse("10pt");
            lbel.Text = value;
        }
    }

    /// <summary>
    /// 公历转为农历的函数
    /// </summary>
    /// <remarks></remarks>
    /// <example></example>
    /// <param name="solarDateTime">公历日期</param>
    /// <returns>农历的日期</returns>
    public static string SolarToChineseLunisolarDate(DateTime solarDateTime)
    {
        System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar();

        int year = cal.GetYear(solarDateTime);
        int month = cal.GetMonth(solarDateTime);
        int day = cal.GetDayOfMonth(solarDateTime);
        int leapMonth = cal.GetLeapMonth(year);
        return string.Format("农历{0}{1}（{2}）年{3}{4}月{5}{6}"
                            , "甲乙丙丁戊己庚辛壬癸"[(year - 4) % 10]
                            , "子丑寅卯辰巳午未申酉戌亥"[(year - 4) % 12]
                            , "鼠牛虎兔龙蛇马羊猴鸡狗猪"[(year - 4) % 12]
                            , month == leapMonth ? "闰" : ""
                            , "无正二三四五六七八九十冬腊"[leapMonth > 0 && leapMonth <= month ? month - 1 : month]
                            , "初十廿三"[day / 10]
                            , "日一二三四五六七八九"[day % 10]
                            );
    }

    /// <summary>
    /// 将二维数组转换成DataTabel
    /// </summary>
    /// <param name="arr">数组</param>
    /// <returns>DataTabel</returns>
    public static DataTable ConvertToDataTable(string[,] arr)
    {

        DataTable dataSouce = new DataTable();
        for (int i = 0; i < arr.GetLength(1); i++)
        {
            DataColumn newColumn = new DataColumn(i.ToString(), arr[0, 0].GetType());
            dataSouce.Columns.Add(newColumn);
        }
        for (int i = 0; i < arr.GetLength(0); i++)
        {
            DataRow newRow = dataSouce.NewRow();
            for (int j = 0; j < arr.GetLength(1); j++)
            {
                newRow[j.ToString()] = arr[i, j];
            }
            dataSouce.Rows.Add(newRow);
        }
        return dataSouce;
    }

    /// <summary>
    /// 根据短信模版创建短信
    /// </summary>
    /// <param name="Model">模版内容</param>
    /// <param name="BookNo">预定信息</param>
    /// <param name="CareNo">消费卡信息</param>
    /// <param name="MDayID">纪念日信息</param>
    /// <returns>短信内容</returns>
    public static string CreateMessage(string Model, DAL.BLI_Book Book, DAL.BLI_Cards Card, DAL.BLI_Consumer_MDay MDay)
    {
        if (Book != null)
        {
            if (Book.Perid_ID > 0)
            {
                Model = Model.Replace("#Perid_ID#", Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select PeriodName from BI_ConsumePeriod where PeriodID=" + Book.Perid_ID).ToString());
            }
            if (Book.Consumer_ID > 0)
            {
                Model = Model.Replace("#Consumer_ID#", Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select CName from BI_Consumer where ConsumerID=" + Book.Consumer_ID).ToString());
            }
            Model = Model.Replace("#BookDate#", Book.BookDate);
            Model = Model.Replace("#AddTime#", Book.AddTime.ToString());
            Model = Model.Replace("#BookNo#", Book.BookNo);
            Model = Model.Replace("#Money#", Book.Money.ToString());
            Model = Model.Replace("#BookType#", Book.BookType.ToString());
            Model = Model.Replace("#ContractName#", Book.ContractName);
            Model = Model.Replace("#ContractPhoneNum#", Book.ContractPhoneNum);
            if (Book.Operator > 0)
            {
                Model = Model.Replace("#Operator#", Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select UserName from BI_User where UserID=" + Book.Operator).ToString());
            }
        }
        if (Card != null)
        {
            if (Card.Consumer_ID > 0)
            {
                Model = Model.Replace("#Consumer_ID#", Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select CName from BI_Consumer where ConsumerID=" + Card.Consumer_ID).ToString());
            }
            Model = Model.Replace("#CardNo#", Card.CardNo);
            Model = Model.Replace("#Money#", Card.Money.ToString());
        }
        if (MDay != null)
        {
            if (MDay.Consumer_ID > 0)
            {
                Model = Model.Replace("#Consumer_ID#", Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select CName from BI_Consumer where ConsumerID=" + MDay.Consumer_ID).ToString());
            }
            Model = Model.Replace("#MdayDate#", MDay.MdayDate.ToString("M/d"));
            Model = Model.Replace("#Remark#", MDay.Remark);
        }
        return Model;
    }

    /// <summary>
    /// 插入短信
    /// </summary>
    /// <param name="ConsumerID">客户编号</param>
    /// <param name="SendTime">插人时间</param>
    /// <param name="content">短信内容</param>
    /// <param name="SMSCLassId">类型编号</param>
    public static void InsertSMS(long ConsumerID, DateTime SendTime, string content, int SMSCLassId)
    {
        DAL.CMSDALDataContext DataSession = new DAL.CMSDALDataContext();
        DAL.BLI_SMS_SendDetail message = new DAL.BLI_SMS_SendDetail();
        message.Consumer_ID = ConsumerID;
        message.SMSContent = content;
        message.SendTime = SendTime;
        message.SMSClass_ID = SMSCLassId;
        DataSession.BLI_SMS_SendDetail.InsertOnSubmit(message);
        DataSession.SubmitChanges();
    }

    /// <summary>
    /// 将预定信息插入或更新到餐饮系统预定表dz_mast中
    /// </summary>
    /// <param name="Book">预定信息</param>
    /// <returns>是否成功</returns>
    public static int InsertMast(DAL.BLI_Book Book)
    {
        //string cString = "Data Source=.;Initial Catalog=txl;User ID=sa;Password=sa;";
        string cString = System.Configuration.ConfigurationManager.ConnectionStrings["SourceConnectionString"].ConnectionString;
        SqlParameter[] sqlPara = new SqlParameter[] { new SqlParameter("@BookNo", Book.BookID.ToString()), 
                                                      new SqlParameter("@fdate", Book.BookDate),
                                                      new SqlParameter("@GuestName",Book.ContractName),
                                                      new SqlParameter("@GuestTele",Book.ContractPhoneNum),
                                                      new SqlParameter("@bookmoney",Book.Money),
                                                      new SqlParameter("@djsjNo",Book.BookNo),
                                                      new SqlParameter("@MakeDate",Book.AddTime)};
        //检查餐饮系统预定表中是否已经存在该预定
        object o = Frame.DataHelper.CssSQLHelper.CssExecuteScalar(cString, CommandType.Text, "select BookNo from dz_mast where BookNo ='" + Book.BookID.ToString() + "'");
        if (o == null)
        {
            return Frame.DataHelper.CssSQLHelper.CssExecuteNonQuery(cString, CommandType.Text, @"INSERT INTO dz_mast (BookNo, fdate, GuestName,GuestTele,bookmoney,djsjNo,bookmoney_use,MakeDate)
                                                                                                              VALUES (@BookNo,@fdate,@GuestName,@GuestTele,@bookmoney,@djsjNo,-1,@MakeDate)", sqlPara);
        }
        else
        {
            return Frame.DataHelper.CssSQLHelper.CssExecuteNonQuery(cString, CommandType.Text, @"UPDATE dz_mast SET fdate = @fdate, 
                                                                                                                    GuestName = @GuestName,
                                                                                                                    GuestTele = @GuestTele,
                                                                                                                    bookmoney = @bookmoney,
                                                                                                                    djsjNo = @djsjNo,
                                                                                                                    MakeDate = @MakeDate
                                                                                                              WHERE BookNo = @BookNo", sqlPara);
        }

    }
}